#!/usr/bin/env bash
# nowplay - a simple script to get track information from cmus
# Code inspired by content found at http://v3gard.com/2011/01/getting-cmus-to-cooperate-with-conky/

# Copyright ©2017-2023 Jason Trunks
# https://notabug.org/JasKinasis/cmus-scripts

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.


# Determine whether cmus is running
if ps -C cmus > /dev/null; then
  # Get information about cmus current state
  data="$(cmus-remote -Q)"

  # Parse the data and determine the status of the player
  playStatus=$(echo "$data" | awk '/^status / {gsub("status ", "");print}')
  # If not stopped - display the details of the currently playing/paused track
  if [[ $playStatus != "stopped" ]]; then
    artist=$(echo "$data" | awk '/^tag artist / {gsub("tag artist ", "");print}')
    title=$(echo "$data" | awk '/^tag title / {gsub("tag title ", "");print}')

	echo "Now $playStatus: $artist - $title"; 
  else  # output the status of the player
    echo "cmus is $playStatus";
  fi
else echo "cmus is not running!"; #output nothing if cmus is not running
fi

